<?php
	require_once(realpath(dirname(__FILE__)) . "/Baseobj.class.php");
	require_once(realpath(dirname(__FILE__)) . "/Variable.class.php");
	require_once(realpath(dirname(__FILE__)) . "/Reecriture.class.php");
	require_once(realpath(dirname(__FILE__)) . "/../fonctions/nettoyage.php");
	require_once(realpath(dirname(__FILE__)) . "/../fonctions/hierarchie.php");
	require_once(realpath(dirname(__FILE__)) . "/../fonctions/url.php");
	
	class Contenudesc extends Baseobj{

		var $id;
		var $contenu;
		var $lang;
		var $titre;
		var $chapo;
		var $description;
		var $postscriptum;

		const TABLE="contenudesc";
		var $table=self::TABLE;

		var $bddvars=array("id", "contenu", "titre", "chapo", "description", "lang", "postscriptum");

		function __construct($contenu = 0, $lang = 1){
			parent::__construct();

			if($contenu > 0)
 			  $this->charger($contenu, $lang);
		}

		function charger($contenu, $lang=1){
   			return $this->getVars("select * from $this->table where contenu=\"$contenu\" and lang=\"$lang\"");
		}

		function charger_id($id){
		     return $this->getVars("select * from $this->table where id=\"$id\"");
		 }
		
		function getUrl($id_dossier){
			$urlsite = new Variable("urlsite");
			$rewrite = new Variable("rewrite");
			if($rewrite->valeur){
				$reecriture = new Reecriture();
				$reecriture->charger_param("contenu", "&id_contenu=" . $this->contenu . "&id_dossier=" . $id_dossier, $this->lang, 1);
				return $urlsite->valeur ."/".$reecriture->url;
			}
			else
				return $urlsite->valeur . "/?fond=contenu&amp;id_contenu=" . "$this->contenu&amp;id_dossier=" . $id_dossier;
			
		}

		function reecrire($url = ""){
		
			$contenu = new Contenu();
			$contenu->charger($this->contenu);
	
			if($url == ""){
				$dossierdesc = new Dossierdesc();
				$dossierdesc->charger($contenu->dossier, $this->lang);

				$url = $this->contenu . "-" . $dossierdesc->titre . "-" . $this->titre . ".html";
			}
	
			$url = eregurl($url);

			$param = "&id_contenu=" . $this->contenu . "&id_dossier=" . $contenu->dossier;
			
			$test = new Reecriture();
			if($test->charger($url, $this->lang))
				return 0;
				
			$reecriture = new Reecriture();
			$reecriture->charger_param("contenu", $param, $this->lang, 1);
			if($reecriture->url == $url)
				return 0;
				
			$reecriture->actif = 0;
			$reecriture->maj();
			
			$reecriture = new Reecriture();
			$reecriture->fond = "contenu";
			$reecriture->url = $url;
			$reecriture->param = $param;
			$reecriture->lang = $this->lang;	
			$reecriture->actif = 1;	
			$reecriture->add();			
		}
	}

?>